package com.wanda.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.executor.ReuseExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanda.model.JsonGeneralModel;
import com.wanda.model.JsonModelGeneral;
import com.wanda.model.MenuModel;
import com.wanda.model.ProjectModel;
import com.wanda.model.Role_MenuModel;
import com.wanda.model.User_Role_OrganizationModel;
import com.wanda.model.User_functionModel;
import com.wanda.model.Wd_FunctionModel;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.MenuService;
import com.wanda.service.Role_MenuService;
import com.wanda.service.User_functionService;
import com.wanda.service.Wd_FunctionService;
import com.wanda.service.Wd_UserService;

@Controller
public class Wd_UserController {
       
	@Resource
	private Wd_UserService wd_UserService;
	
	@Resource
	private User_functionService user_functionService;

	@Resource
	private MenuService menuCountroller;

	@Resource
	private Role_MenuService role_MenuService;

    
	@Autowired
	private Wd_FunctionService wd_FunctionService;

	// 登录
	@RequestMapping(value = "/qt/selectLogin")
	public @ResponseBody Map<String, Object> selectLogin(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String tel = request.getParameter("tel");
		String password = request.getParameter("password");
		String date = request.getParameter("KEYDATA");
		Wd_UserModel user = new Wd_UserModel();

		user.setPassword(password);
		user.setTel(tel);
		System.err.println("password" + password);
		System.err.println("tel" + tel);
		System.err.println("date" + date);
		Wd_UserModel userModel = wd_UserService.selectLogin(user);

		String yzm =(String)request.getSession().getAttribute("yzm");

		System.out.println("yzm:" + yzm);
		
		if (!date.equals(yzm)) {
			
			map.put("result", "codeerror");
		} else if (userModel == null || "".equals(userModel)) {
			map.put("result", "usererror");
		} else {
			request.getSession().setAttribute("bean", userModel);
			Role_MenuModel Role = new Role_MenuModel();
			Role.setUser_id(userModel.getUser_id());
			Role.setPid("1");
			List<Role_MenuModel> menu1 = new ArrayList<Role_MenuModel>();
			menu1 = role_MenuService.selectAllRoot_view(Role);
		   for(Role_MenuModel menu:menu1){
			   Role.setUser_id(userModel.getUser_id());
			  
			   Role.setPid(menu.getMenu_id());
			
			   menu.setSubMenu(role_MenuService.selectAllRoot_view(Role));
		   }
			
			
			request.getSession().setAttribute("menuList", menu1);
			map.put("result", "success");
		}
		
		List<Wd_FunctionModel> functions1 =wd_FunctionService.selectAll();
		List<String> functions=new ArrayList<String>();
		for(Wd_FunctionModel function:functions1){
			functions.add(function.getFunction_path());
		}
		request.getSession().setAttribute("functions", functions);
		if(userModel!=null){
		List<User_functionModel> function1 =user_functionService.selectByUserId(userModel.getUser_id());
		List<String> function=new ArrayList<String>();
		for(User_functionModel functio:function1){
			function.add(functio.getFunction_path());
		}
		request.getSession().setAttribute("function", function);
		}
		
		System.out.println("按道理成功了 ");
		return map;
	}

	@RequestMapping(value = "ht/selectBySql.do")
	public @ResponseBody JsonGeneralModel selectBySql(String SearchSql) {
		System.out.println("SQL查询");
		System.err.println("SearchSql:" + SearchSql);
		JsonGeneralModel<Wd_UserModel> jsonModel = new JsonGeneralModel();
		List<Wd_UserModel> wd_User = null;
		try {
			// delete_state="0";
			wd_User = wd_UserService.selectBySql(SearchSql);
			jsonModel.setRows(wd_User);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonModel;
	}
	
	
	@RequestMapping(value = "ht/selectAll.do")
	public @ResponseBody JsonGeneralModel selectall() {
		System.out.println("SQL查询");

		JsonGeneralModel<User_Role_OrganizationModel> jsonModel = new JsonGeneralModel();
		List<User_Role_OrganizationModel> wd_User = null;
		try {
			// delete_state="0";
			wd_User = wd_UserService.selectuserAll();
			jsonModel.setRows(wd_User);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonModel;
	}
	
	
	@RequestMapping(value ="addUser")
	public @ResponseBody ModelAndView addUser(HttpServletRequest request,HttpServletResponse response) {
		int row =0;
		JsonGeneralModel<User_Role_OrganizationModel> jsonModel = new JsonGeneralModel();
		Wd_UserModel   UserModel = new Wd_UserModel();
	    String mima=request.getParameter("mima");
		String name=request.getParameter("name");
		String tle =request.getParameter("tel");
		String jueshi=request.getParameter("jueshi");
		String jigou=request.getParameter("jigou");
		try {
			UserModel.setPassword(mima);
			UserModel.setName(name);
			UserModel.setTel(tle);   
			UserModel.setOrganization_id(jigou);
			int c=0;
			c = Integer.parseInt(jueshi);
			UserModel.setRol_id(c);
		    row =wd_UserService.addUser(UserModel);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		  PrintWriter out=null;
		  String info="";
	
			 
			try {
				if(row>0){
				out = response.getWriter();
			      info="<script>alert('添加成功');</script>";
				}else{
					info="<script>alert('添加失败');</script>";
				}
				 out.write(info);
			} catch (IOException e) {
				e.printStackTrace();
			}
			ModelAndView mav = new ModelAndView();
			mav.setViewName("/qt/listUser.jsp");
			return mav; 
	}
	
	
	@RequestMapping(value = "deleteuser")
	public @ResponseBody Map<String,Object> delete(String user_id) {
		Map<String,Object> map = new HashMap<String,Object>(); 
		if(wd_UserService.deleteUser(user_id)>0){
	    	map.put("msg", "删除成功");
		}else{
			map.put("msg","删除失败");
		}
		return map;
	}
	
	
	@RequestMapping(value = "selectbyid")
	public @ResponseBody Map<String,Object> selectbyid(@Param(value = "user_id") String user_id) {
		System.err.println("ddddddddddddddddddddddddddd"+user_id);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put("msg", wd_UserService.selectById(user_id));
		return map;
    }
	
	

	@RequestMapping(value = "updateuser")
	public @ResponseBody ModelAndView updateuser(Wd_UserModel   UserModel ) {
		
		System.err.println("a111111111111111111111111111111111111111111"+UserModel.getUser_id());
	
		Map<String,Object> map = new HashMap<String,Object>(); 
		
		map.put("msg", wd_UserService.updateUser(UserModel));
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/qt/listUser.jsp");
		return mav; 
    }   
	
	
	
}
	


